package com.st.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.st.pojo.User;
import com.st.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name="扩展mybatis-plus功能")
@RestController
@RequestMapping(path = "wapper")
public class WapperController {

    @Resource
    private UserService userService;

    @Operation(summary = "测试原始mybatis接口")
    @RequestMapping(path = "foo",method = RequestMethod.GET)
    public String foo(){
        //会将user表中所有的数据查询出来
        List<User> userList = userService.list();
        return "ok";
    }


    @Operation(summary = "测试mybatis wapper功能:like")
    @RequestMapping(path = "wapper",method = RequestMethod.GET)
    public String wapper(){
        //会将user表中所有符合条件的的数据查询出来
        //1.创建查询组装器  要查哪张表的数据，wapper就需要指定该表对应实体类 作为泛型
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.like("wx_user_name","xxx");
        List<User> userList = userService.list(qw);
        return "ok";
    }


    @Operation(summary = "测试mybatis wapper功能:select")
    @RequestMapping(path = "select",method = RequestMethod.GET)
    public User select(@RequestParam(User.COL_ID) Integer uid){
        //会将user表中所有符合条件的的数据查询出来
        //1.创建查询组装器  要查哪张表的数据，wapper就需要指定该表对应实体类 作为泛型
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.select(User.COL_WX_USER_NAME);
        qw.eq(User.COL_ID,uid);//指定条件为： id = #{uid}
        qw.clear(); //清除之前设置的qw条件
        qw.ge(User.COL_ID,5); //指定条件为： id >= 5
        qw.le("aga",18); //指定条件为： age <= 18
        qw.in(User.COL_ID,1,2,3,4,5,6); //指定条件为：id 在 1，2，3，4，5，6 中
        qw.isNotNull(User.COL_WX_USER_NAME); // 指定条件为： wx_user_name ！= null
        qw.likeLeft(User.COL_WX_USER_NAME,"xxx"); // 指定条件为：wx_user_name like '%xxx'
        qw.likeRight(User.COL_WX_USER_NAME,"xxx");// 指定条件为：wx_user_name like 'xxx%'
        qw.ne(User.COL_ID,1);//指定条件为： id != 1
        qw.or(); // 在两个条件之间指定逻辑关系为 或
        qw.orderByDesc(User.COL_ID); //指定排序规则为： order by id desc
        qw.orderByAsc(User.COL_ID); //指定排序规则为： order by id
        qw.between(User.COL_ID,1,10); // id between 1 and 10
        qw.notBetween(User.COL_ID,1,10); // id not between 1 and 10

        User user = userService.getOne(qw);
        return user;
    }


    @Operation(summary = "测试mybatis wapper功能:updateWrapper")
    @RequestMapping(path = "updateWrapper",method = RequestMethod.POST)
    public String updateWrapper(){
        //1.创建修改构造器
        UpdateWrapper<User> uw = new UpdateWrapper<>();
        //2.构建条件说明及修改说明
        uw.set(User.COL_WX_USER_NAME,"xxx"); // update t_user set wx_user_name = 'xxx'
        uw.eq(User.COL_ID,1);// update t_user set wx_user_name = 'xxx' where id = 1
        uw.or(); // update t_user set wx_user_name = 'xxx' where id = 1 or
        uw.notLike(User.COL_PHONE,"123"); //update t_user set wx_user_name = 'xxx' where id = 1 or phone not like '%123%'
        //3.使用
        boolean update = userService.update(uw);


        return "ok";
    }
}
